package top.rainbowecho.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.rainbowecho.article.mapper.ManagementMapper;
import top.rainbowecho.article.service.IManagementService;
import top.rainbowecho.article.util.RedisProxy;
import top.rainbowecho.common.util.CacheKey;
import top.rainbowecho.model.percona.Management;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 存储网站管理的一些零碎信息 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2020-07-25
 */
@Service
public class ManagementServiceImpl extends ServiceImpl<ManagementMapper, Management> implements IManagementService {
    @Resource
    private ManagementMapper managementMapper;

    @Resource
    private RedisProxy redisProxy;

    @Override
    @Transactional
    public List<Map<String, String>> getAllManagement() {
        return this.redisProxy.getFromCache(CacheKey.MANAGEMENT, () -> {
            List<Management> managements = this.managementMapper.selectList(null);
            Map<String, String> val = new HashMap<>();
            for (Management management : managements) {
                val.put(management.getName(), management.getValue());
            }
            List<Map<String, String>> result = new ArrayList<>();
            result.add(val);
            return result;
        });
    }

    @Override
    @Transactional
    public void saveManagement(Management management) {
        this.redisProxy.evict(CacheKey.MANAGEMENT);
        this.managementMapper.insert(management);
    }
}
